
global root_dir = "`1'"

include "$root_dir/code/config/config.do"

cap noi log using ${log_dir}/orbis_patents.log, replace name(dat)

*Handle empty arguments
global arg1 = cond("`2'" == "___EMPTY___", "", "`2'")
global arg2 = cond("`3'" == "___EMPTY___", "", "`3'")
global arg3 = cond("`4'" == "___EMPTY___", "", "`4'")
global arg4 = cond("`5'" == "___EMPTY___", "", "`5'")

if "$arg1" != "" {
    global weight_category "$arg1"
    di "Weight category: ${weight_category}"
}

if "$arg2" != "" {
    global weight_versions "$arg2"
    di "Weight versions: ${weight_versions}"
}

if "$arg3" != "" {
    global weight_window "$arg3"
    di "Weight window: ${weight_window}"
}

if "$arg4" != "" {
	global wtype "$arg4"
}
di "${wtype}"

capture noi {

global orbis_dir = "${commondata_dir}/orbis_patents"
mkdir "${orbis_dir}/dta"
mkdir "${orbis_dir}/dta_names"


local files : dir "${orbis_dir}/utf8" files "*.txt"


foreach file in `files' {
	import delimited using `file', clear varnames(1)
	ren applicationnumber Applicationnumber
	ren currentownersbvdid BvDIDnumber
	replace Applicationnumber = Applicationnumber[_n-1] if Applicationnumber==""
	drop if BvDIDnumber==""
	drop if Applicationnumber==""
	keep Applicationnumber BvDIDnumber
	duplicates drop
	save ${orbis_dir}/dta/`file'.dta, replace
}



local files : dir "${orbis_dir}/dta" files "*.dta"

use "${orbis_dir}/dta/data_0000000001_0000080000.txt.dta", clear
foreach file in `files' {
	append using `file'
}
duplicates drop
gen appln_date = substr(Applicationnumber,-8,.)
gen appln_year = substr(appln_date,1,4)
gen ll=length(Applicationnumber)
gen ll2=ll-9
gen appln_nr_orbis = substr(Applicationnumber,1,ll2)
ren appln_nr_orbis appln_nr_epodoc

mmerge appln_nr_epodoc using ${commondata_dir}/patstat_2018b/appln_info.dta, ukeep(appln_id ipr_type appln_auth appln_nr) unmatched(master)
drop ll* Applicationnumber _m
destring, replace
duplicates drop

save ${commondata_dir}/orbis_patents/Orbis_patents_list_2017.dta, replace
keep BvD
save ${orbis_dir}/Orbis_patents_2017_BVDID_list.dta, replace




* Name of companies

local files : dir "${orbis_dir}/utf8" files "*.txt"

foreach file in `files' {
	import delimited using `file', clear varnames(1)
	keep currentowners currentownersbvdid
	ren currentowners CompanyName
	ren currentownersbvdid BvDIDnumber
	drop if BvDIDnumber==""
	duplicates drop
	save ${orbis_dir}/dta_names/`file'.dta, replace
}

local files : dir "${orbis_dir}/dta_names" files "*.dta"

use "${orbis_dir}/dta/data_0000000001_0000080000.txt.dta", clear
foreach file in `files' {
	append using `file'
}
duplicates drop
compress
save ${orbis_dir}/Orbis_patents_2017_company_names.dta, replace
outsheet using ${orbis_dir}/Orbis_patents_2017_company_names.txt

* NACE codes

foreach file in 1 2 3 4 {
	import delimited using ${orbis_dir}/nace_orbis_2017/patents_bvd_nace`file'.txt, clear varnames(1) stringcols(_all) 
	keep bvdidnumber nacerev2corecode4digits countryisocode
	ren bvdidnumber BvDIDnumber
	ren nacerev2corecode4digits NACE
	ren countryisocode country
	drop if BvDIDnumber==""
	duplicates drop
	save ${orbis_dir}/nace_orbis_2017/patents_bvd_nace`file'.dta, replace
}

use ${orbis_dir}/nace_orbis_2017/patents_bvd_nace1.dta, clear
foreach file in 2 3 4 {
	append using ${orbis_dir}/nace_orbis_2017/patents_bvd_nace`file'.dta
}
duplicates drop
sort BvD
compress
save ${orbis_dir}/Orbis_patents_2017_nace_country.dta, replace


use ${orbis_dir}\nace_duo_guo_status\orbis_AF01.dta, clear
keep bvd nace status
drop if nace==""
foreach xxx in FECA01 FECA02 FECA03 FECA04 FECA05 FECA06 LA01 LA02 LA03 LA04 LA05 EE01 EE02 EE03 EE04 EE05 EE06 EE07 EE08 EE09 EE10 EE11 EE13 ME01 OC01 OC02 OC03 WE01 WE02 WE03 WE04 WE05a WE05b WE06 WE07a WE07b NA01 NA02 NA03 NA04 NA05 NA06 NA07 NA08 NA09 NA10 NA11 NA12 NA13 NA14 NA15 NA16 NA17 NA18 NA19 NA20 NA21 NA22 NA23{
	append using ${orbis_dir}\nace_duo_guo_status\orbis_`xxx'.dta, keep(bvd nace status)
	drop if nace==""
}
duplicates drop
mmerge bvd using ${orbis_dir}/Orbis_patents_2017_BVDID_list.dta, umatch(BvD) unmatched(none)
drop _m
save ${orbis_dir}/Orbis_patents_2017_nace_V2.dta, replace



* FIX PROBLEM WITH JP BVDIDs which changed in May 2017...

import delimited using ${orbis_dir}/nace_orbis_2017/JP_BVD2.txt, clear varnames(1) stringcols(_all) 
keep bvdidnumber ipidentificationnumber
drop if ipidentificationnumber==""
gen ll=length(ipidentificationnumber)
keep if ll==9
gen bvdid_old = "JP" + ipidentificationnumber
duplicates drop
keep bvd*
compress
save ${orbis_dir}/nace_orbis_2017/JP_oldBVDIDs.dta, replace

use ${orbis_dir}/Orbis_patents_2017_BVDID_list.dta, clear
mmerge BvD using ${orbis_dir}/Orbis_patents_2017_nace_country.dta, unmatched(master)
mmerge BvD using ${orbis_dir}/bvdid_nace_country.dta, unmatched(master) ukeep(NACERev2Corecode4digits)
mmerge BvD using ${orbis_dir}/Orbis_patents_2017_nace_V2.dta, unmatched(master) umatch(bvd) 
replace NACE=nacerev2corecode4digits if NACE=="" & nacerev2corecode4digits!=""
replace NACE=NACERev2Corecode4digits if NACE=="" & NACERev2Corecode4digits!=""
keep BvD NACE country
mmerge BvD using ${orbis_dir}/nace_orbis_2017/JP_oldBVDIDs.dta, unmatched(master) umatch(bvdid_old) 
drop _m
ren NACE NACECODE
mmerge bvdidnumber using ${orbis_dir}/Orbis_patents_2017_nace_country.dta, unmatched(master) umatch(BvD) 
replace NACECODE=NACE if NACECODE=="" & NACE!=""
keep BvD NACECODE country
ren NACECODE NACE
duplicates drop
sort BvD
ren NACE nace
gen cc=substr(BvD, 1, 2)
br if country==""
replace country=cc if country==""
save ${orbis_dir}/Orbis_patents_2017_nace_country_updated.dta, replace


}
if _rc == 0 {
    display "Execution finished successfully."
}
else {
    display "Execution finished with errors."
}

cap log close dat